SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[MoodysRatingDistribution]
(
    @FundID VARCHAR(10),
    @StartDate AS DATETIME 

) AS
BEGIN
DECLARE @ExportID   int
DECLARE @FundMktVal DECIMAL(38,10)
 

 

 --SET @StartDate = '3/29/2013'
 --SET @FundID    = 'FSIC'
 SET @ExportID  =
 (SELECT MAX(ExportId)
 FROM    [FSFundModel].[Reporting].Export
 WHERE   FundId  = @FundID
 AND     NAVDate = @StartDate)
 
 SET @FundMktVal =
 (SELECT f.MktVal
 FROM    [FSFundModel].[Reporting].Fund f
 WHERE   FundId   = @FundID
 AND     ExportId = @ExportID)
 
 SELECT     i.MoodysRating
          , SUM(a.MktVal) / @FundMktVal AS Pct
 
 
 
 FROM       [FSFundModel].[Reporting].asset a
            
            INNER JOIN [FSFundModel].[Reporting].Instrument i
            ON         a.ExportId     = i.ExportId
            AND        a.InstrumentId = i.InstrumentId
            INNER JOIN [FSFundModel].[Reporting].InstrumentDefinition id
            ON         a.ExportId  = id.ExportId
            AND        i.InstDefId = id.InstDefId
 WHERE      a.IsActiveEndOfDay     = 1
 AND        a.ExportId             = @ExportID
 
 
 GROUP BY   i.MoodysRating
END

GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Live/CharlieWaterman', 'SCHEMA', N'dbo', 'PROCEDURE', N'MoodysRatingDistribution', NULL, NULL
GO
